package com.woniu.repaircenter.service.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.annotation.RequirePermission;
import com.woniu.annotation.RequireRole;
import com.woniu.repaircenter.models.dto.RepairDTO;
import com.woniu.repaircenter.models.dto.RepairListDTO;
import com.woniu.repaircenter.models.form.*;
import com.woniu.repaircenter.models.param.*;
import com.woniu.repaircenter.service.service.RepairService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 维修记录表 前端控制器
 * </p>
 *
 * @author 吕雨泽
 * @since 2024年12月06日
 */
@RestController
@RequestMapping("/repair")
@Api(tags = "维修记录表")
public class RepairController {

    @Autowired
    private RepairService repairService;

    /**
     * 新增维修记录
     * @param arf 维修记录
     * @return 新增结果
     * @throws Exception 异常
     */
    @PostMapping("/addRepair")
    @RequireRole("学生")
    @ApiOperation(value = "新增维修记录", notes = "新增维修记录")
    public Result addRepair(@RequestHeader("token") String token,@Valid @RequestBody @ApiParam(name = "arf", value = "维修记录", required = true) AddRepairForm arf) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        Integer repair = repairService.addRepair(BeanUtil.toBean(arf, AddRepairParam.class),userId);
        if (repair == 1) {
            return Result.getResult().setMsg("工单已提交").setCode(200);
        } else {
            return Result.getResult().setMsg("工单提交失败").setCode(500);
        }
    }

    /**
     * 修改维修记录派单
     * @param udrf 修改参数
     * @param token 用户令牌
     * @return 修改结果
     * @throws Exception 异常
     */
    @PutMapping("/dispatchRepair")
    @ApiOperation(value = "分配维修工单", notes = "分配维修工单")
    @RequireRole("管理员")
    @RequirePermission("分配维修工单")
    public Result dispatchRepair(
            @Valid @RequestBody @ApiParam(name = "udrf", value = "分配工单参数", required = true)UpdateDispatchRepairForm udrf,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String adminName = claims.get("adminName").toString();
        Integer repair = repairService.dispatchRepair(BeanUtil.toBean(udrf, UpdateDispatchRepairParam.class), adminName);
        if (repair == 1) {
            return Result.getResult().setMsg("派单成功").setCode(200);
        }
        return Result.getResult().setMsg("派单失败").setCode(500);
    }

    /**
     * 维修工单进行中
     * @param repairNum 维修单号
     * @param token 用户令牌
     * @return  进行中结果
     * @throws Exception 异常
     */
    @PutMapping("/underwayRepair")
    @ApiOperation(value = "维修工单进行中", notes = "维修工单进行中")
    public Result underwayRepair(
            @RequestParam @ApiParam(name = "repairNum", value = "维修单号", required = true) String repairNum,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String userName = claims.get("userName").toString();
        Integer repair = repairService.underwayRepair(repairNum, userName);
        if (repair == 1) {
            return Result.getResult().setMsg("进行中").setCode(200);
        }
        return Result.getResult().setMsg("失败").setCode(500);
    }

    /**
     * 完成维修工单
     * @param ufrf 维修单号
     * @param token 用户令牌
     * @return  完成结果
     * @throws Exception 异常
     */
    @PutMapping("/finishRepair")
    @ApiOperation(value = "完成维修工单", notes = "完成维修工单")
    public Result finishRepair(
            @Valid@RequestBody @ApiParam(name = "repairNum", value = "维修单号", required = true) UpdateFinishRepairForm ufrf,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String userName = claims.get("userName").toString();
        Integer repair = repairService.finishRepair(BeanUtil.toBean(ufrf, UpdateFinishRepairParam.class), userName);
        if (repair == 1) {
            return Result.getResult().setMsg("完成成功").setCode(200);
        }
        return Result.getResult().setMsg("完成失败").setCode(500);
    }

    /**
     * 驳回工单
     * @param urrf 驳回工单参数
     * @param token 用户令牌
     * @return  驳回结果
     * @throws Exception 异常
     */
    @PutMapping("/rejectRepair")
    @ApiOperation(value = "驳回工单", notes = "驳回工单")
    @RequireRole("管理员")
    @RequirePermission("驳回维修工单")
    public Result rejectRepair(
            @Valid @RequestBody @ApiParam(name = "ufrf", value = "驳回工单参数", required = true)UpdateRejectRepairForm urrf,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String adminName = claims.get("adminName").toString();
        Integer repair = repairService.rejectRepair(BeanUtil.toBean(urrf, UpdateRejectRepairParam.class), adminName);
        if (repair == 1) {
            return Result.getResult().setMsg("驳回成功").setCode(200);
        }
        return Result.getResult().setMsg("驳回失败").setCode(500);
    }

    /**
     * 取消工单
     * @param urrf 取消工单参数
     * @param token 用户令牌
     * @return  取消结果
     * @throws Exception 异常
     */
    @PutMapping("/cancelRepair")
    @ApiOperation(value = "取消工单", notes = "取消工单")
    public Result cancelRepair(
            @Valid @RequestBody @ApiParam(name = "ufrf", value = "取消工单参数", required = true)UpdateRejectRepairForm urrf,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String userName = claims.get("userName").toString();
        Integer repair = repairService.cancelRepair(BeanUtil.toBean(urrf, UpdateRejectRepairParam.class), userName);
        if (repair == 1) {
            return Result.getResult().setMsg("取消成功").setCode(200);
        }
        return Result.getResult().setMsg("取消失败").setCode(500);
    }

    /**
     * 查询维修记录
     * @param srf 查询条件
     * @return 维修记录列表
     * @throws Exception 异常
     */
    @PostMapping("/selectRepairList")
    @ApiOperation(value = "查询维修记录", notes = "查询维修记录")
    public Result selectRepairList(@RequestBody @ApiParam(name = "srf", value = "查询条件", required = true) SelectRepairForm srf) throws Exception {
        PageInfo<RepairListDTO> repairDTOS = repairService.selectRepairList(BeanUtil.toBean(srf, SelectRepairParam.class));
        return Result.getResult(PageInfo.class).setMsg("查询成功").setCode(200).setData(repairDTOS);
    }

    /**
     * 根据维修工人查询维修记录
     * @param srwf 维修工人表单
     * @return  维修记录列表
     * @throws Exception 异常
     */
    @PostMapping("/selectRepairByWorker")
    @ApiOperation(value = "根据维修工人查询维修记录", notes = "根据维修工人查询维修记录")
    public Result selectRepairByWorker(
            @RequestBody @ApiParam(name = "srwf", value = "维修工人表单", required = true) SelectRepairWorkerForm srwf,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        PageInfo<RepairListDTO> repairDTOS = repairService.selectRepairByWorker(BeanUtil.toBean(srwf, SelectRepairWorkerParam.class),userId);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setCode(200).setData(repairDTOS);
    }

    /**
     * 根据用户id查询维修记录
     * @param sruf 用户表单
     * @return  维修记录列表
     * @throws Exception 异常
     */
    @PostMapping("/selectRepairByUserId")
    @ApiOperation(value = "根据用户id查询维修记录", notes = "根据用户id查询维修记录")
    public Result selectRepairByUserId(
            @RequestBody @ApiParam(name = "sruf", value = "用户表单", required = true) SelectRepairUserForm sruf,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        PageInfo<RepairListDTO> repairDTOS = repairService.selectRepairByUserId(BeanUtil.toBean(sruf, SelectRepairUserParam.class),userId);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setCode(200).setData(repairDTOS);
    }

    /**
     * 根据id查询维修记录
     * @param repairId 维修记录id
     * @param repairNum 维修单号
     * @return 维修记录
     * @throws Exception 异常
     */
    @GetMapping("/selectRepairById")
    @ApiOperation(value = "根据id查询维修记录", notes = "根据id查询维修记录")
    public Result selectRepairById(
            @RequestParam @ApiParam(name = "repairId", value = "维修记录id", required = true) Long repairId,
            @RequestParam @ApiParam(name = "repairNum", value = "维修单号", required = true) String repairNum) throws Exception {
        RepairDTO repairDTO = repairService.selectRepairById(repairId, repairNum);
        return Result.getResult(RepairDTO.class).setMsg("查询成功").setCode(200).setData(repairDTO);
    }

    /**
     * 删除维修工单
     * @param repairNum 修改参数
     * @param token 用户令牌
     * @return 修改结果
     * @throws Exception 异常
     */
    @DeleteMapping("/deleteRepair")
    @ApiOperation(value = "删除维修工单", notes = "删除维修工单")
    public Result deleteRepair(
            @RequestParam @ApiParam(name = "repairNum", value = "维修工单号", required = true) String repairNum,
            @RequestHeader("token") @ApiParam(name = "token", value = "用户令牌", required = true) String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        String userName = claims.get("userName").toString();
        Integer repair = repairService.deleteRepair(repairNum,userId,userName);
        if (repair == 1) {
            return Result.getResult().setMsg("删除成功").setCode(200);
        }
        return Result.getResult().setMsg("删除失败").setCode(500);
    }
}
